package com.base.cn.platform.os.controller.mobile.course.open;

import com.base.cn.platform.os.common.MobileBaseController;
import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.count.PageFrom;
import com.base.cn.platform.os.common.utils.count.PageKeyUtils;
import com.base.cn.platform.os.common.utils.course.CourseOpenForm;
import com.base.cn.platform.os.service.course.open.CourseOpenBespeakService;
import com.base.cn.platform.os.service.course.open.CourseOpenService;
import com.base.cn.platform.os.service.manage.config.SysConfigService;
import com.base.cn.platform.os.service.manage.count.PageViewCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * H5免费直播Controller
 * @author zsw
 * @create 2018/6/27
 */
@Controller
public class MobileOpenCourseController extends MobileBaseController {

    @Autowired
    private CourseOpenService courseOpenService;
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private CourseOpenBespeakService courseOpenBespeakService;
    @Autowired
    private PageViewCountService pageViewCountService;

    /**
     * 历史免费直播(免费直播)
     */
    @RequestMapping("/mobile/open/history")
    public String toHistoryOpen() {
        pageViewCountService.addPageCount(PageKeyUtils.OPEN_INDEX, PageFrom.PAGE_FROM_MOBILE);
        return resultPage("/course/open/open-history");
    }

    /**
     * 加载免费直播(免费直播)数据
     *
     * @param model
     * @param request
     * @param currentPage
     * @param params
     * @return
     */
    @RequestMapping("/mobile/open/ajax/load/history")
    public String ajaxHistoryOpen(Model model,
                                  HttpServletRequest request,
                                  @RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage,
                                  @RequestBody Map<String ,Object> params) {
        int year = (int)params.get("year");
        int month = (int)params.get("month");
        int date = (int)params.get("date");
        Date now = DateUtils.parseDate(year + "-" + month + "-" + date, "yyyy-MM-dd");
        HashMap<String, Object> condition = new HashMap<>();
        condition.put("nowDate",now);
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        condition.put("userId",userId);
        condition.put("status",1);
        Map<String, Object> courseOpenList = courseOpenService.findCourseOpenPage(condition, currentPage,null ,false, true, false,true);
        model.addAttribute("dataMap",courseOpenList);
        return resultPage("/course/open/ajax-open-history");
    }

    /**
     * 播放免费直播管理(免费直播)
     *
     * @param openId 免费直播管理id
     * @return 直播路径
     */
    @RequestMapping("/mobile/open/ajax/play")
    @ResponseBody
    public Map<String, Object> play(HttpServletRequest request,
                                    @RequestParam("openId") BigDecimal openId) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return courseOpenService.playOpenLive(openId,userId, CourseOpenForm.MOBILE);
    }

    /**
     * 播放码方式播放回放
     * @param code 播放码
     * @return Map<String,Object>
     */
    @RequestMapping("/mobile/uc/open/playBackCode")
    public String playBackCode(Model model,
                               @RequestParam("code") String code,
                               @RequestParam("playType") String playType){
        TreeMap<String,String> configMap = sysConfigService.getSysConfigContext(ConfigName.V96K);
        if(playType.equals("TT")){
            String player = configMap.get("player");
            player = player+"?width=100%&height=100%&videoId="+code;
            model.addAttribute("playUrl",player);
        }else if(playType.equals("CC")){
            TreeMap<String,String> configMapTo = sysConfigService.getSysConfigContext(ConfigName.VLASPROVIDER);
            String player ="";
              if(configMapTo.get("isK96keVideo").equals("ON")){
                   player = configMap.get("player");
                  player = player+"?width=100%&height=100%&videoId="+code;
                }else{
                  player =   "https://p.bokecc.com/player?vid="+code+"&siteid="+configMap.get("ccAppId")+"&autoStart=true&width=100%&height=100%&playerid=E631F464FA6F48D8&playertype=1";
              }
            model.addAttribute("playUrl",player);
        }
        model.addAttribute("playType",playType);
        return resultPage("/course/open/play-back-code");
    }

    /**
     * 预约报名免费直播
     * @param request HttpServletRequest
     * @param openId 免费直播ID
     * @return Map<String,Object>
     */
    @RequestMapping("/mobile/uc/open/bespeakOpen")
    @ResponseBody
    public Map<String,Object> bespeakOpen(HttpServletRequest request,
                                          @RequestParam("openId") BigDecimal openId){
        Map<String,Object> params = new HashMap<>();
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        params.put("userId",userId);
        params.put("openCourseId",openId);
        return courseOpenBespeakService.saveBespeak(params);
    }

}
